var bool = auto_new = false;
var fp = 0;
var un = 0;
var ball_odds = cl_hao = cl_dx = cl_ds = cl_zhdx = cl_zhds = cl_lh = '';
var is_fenpan = true;
var min_money = 1;
var max_money = 99999999;

//限制只能输入1-9纯数字 
function digitOnly($this) {
    var n = $($this);
    var r = /^\+?[1-9][0-9]*$/;
    if (!r.test(n.val())) {
        n.val("");
    }
}

function reset() {
    document.orders.reset();
}

//10秒倒计时
function countdown(waittime) {
    if (waittime <= 0) {
        clearTimeout(cd);
        waittime = 10;
        updateOpenTime();
        auto(1);
        window.parent.account();
    }
    waittime--;
    cd = setTimeout("countdown(" + waittime + ")", 1000);
    $("#countdown").html(waittime + "秒");
}


function updateOpenTime() {
    $.post("class/odds_pk10.php", function (data) {
        if (!data.isopen) {
            clearTimeout(fp);
            clearTimeout(kj);

            $("#open_qihao").html("第 3902" + data.number + " 期");
            loadodds(data.oddslist);
            endtime(data.endtime);
            kjtime(data.opentime);
        }
    }, "json");
}

//盘口信息
function loadinfo() {
    $.post("class/odds_pk10.php", function (data) {
        if (data.opentime > 0) {
            is_fenpan = false;
            if (!data.isopen) {
                is_fenpan = true;
            }
            $("#open_qihao").html("第 " + data.number + " 期");
            ball_odds = data.oddslist;
            loadodds(data.oddslist); //读取类型及赔率
            endtime(data.endtime);   //封盘时间
            kjtime(data.opentime);   //开奖时间
            auto(1);
            countdown(10);
            min_money = data.min_money;
            max_money = data.max_money;
        } else {
            is_fenpan = true;
            close_td();
            return false;
        }
    }, "json");
}

//更新赔率
function loadodds(oddslist) {
    if (oddslist == null || oddslist == "" || is_fenpan) {
        close_td();
        return false;
    }
    for (var b = 1; b <= 11; b++) {
        if (b <= 10) {
            for (var s = 1; s <= 16; s++) {
                odds = oddslist.ball[b][s];
                $("#ball_" + b + "_h" + s).html(odds);
                loadinput(b, s);
            }
        } else {
            for (var s = 1; s <= 21; s++) {
                odds = oddslist.ball[b][s];
                $("#ball_" + b + "_h" + s).html(odds);
                loadinput(b, s);
            }
        }
    }
    $(".remove_gan").html("");
}

//更新投注框
function loadinput(ball, s) {
    //console.log(s);
    b = "ball_" + ball + "_" + s;
    n = "<input name=\"" + b + "\" id=\"" + b + "\" class=\"inp1\" onkeyup=\"digitOnly(this)\" onfocus=\"this.className='inp1m'\" onblur=\"this.className='inp1';\" type=\"text\" maxLength=\"5\"/>"
    $("#ball_" + ball + "_t" + s).html(n);
}

//封盘时间
function endtime(iTime) {
    var iMinute, iSecond;
    var sMinute = "", sSecond = "", sTime = "";
    iMinute = parseInt((iTime / 60) % 60);
    if (iMinute == 0) {
        sMinute = "00";
    }
    if (iMinute > 0 && iMinute < 10) {
        sMinute = "0" + iMinute;
    }
    if (iMinute > 10) {
        sMinute = iMinute.toString();
    }
    iSecond = parseInt(iTime % 60);
    if (iSecond >= 0 && iSecond < 10) {
        sSecond = "0" + iSecond;
    }
    if (iSecond >= 10) {
        sSecond = iSecond.toString();
    }
    sTime = sMinute + sSecond;

    if (iTime < 0) {
        //将原框类数据隐藏及清空
        $(".mask", window.parent.document).hide();
        $("#confirm_result", window.parent.document).empty();

        clearTimeout(fp);
        is_fenpan = true;
        close_td();
    } else {
        iTime--;
        var t = 'time';
        $("#fptime").html(sTime.substr(0, 2) + ":" + sTime.substr(2, 3));
        fp = setTimeout("endtime(" + iTime + ")", 1000);
    }
}

//开奖时间
function kjtime(iTime) {
    var iMinute, iSecond;
    var sMinute = "", sSecond = "", sTime = "";
    iMinute = parseInt((iTime / 60) % 60);
    if (iMinute == 0) {
        sMinute = "00";
    }
    if (iMinute > 0 && iMinute < 10) {
        sMinute = "0" + iMinute;
    }
    if (iMinute > 10) {
        sMinute = iMinute.toString();
    }
    iSecond = parseInt(iTime % 60);
    if (iSecond >= 0 && iSecond < 10) {
        sSecond = "0" + iSecond;
    }
    if (iSecond >= 10) {
        sSecond = iSecond.toString();
    }
    sTime = sMinute + sSecond;

    if (iTime < 0) {
        //新开盘时间清空上一期下注记录
        $(".order", window.parent.document).remove();
        $("#tzinfo", window.parent.document).hide();

        clearTimeout(cd);
        clearTimeout(kj);
        loadinfo();
    } else {
        iTime--;
        $("#kjtime").html(sTime.substr(0, 2) + ":" + sTime.substr(2, 3));
        kj = setTimeout("kjtime(" + iTime + ")", 1000);
    }
}

//更新开奖号码
function auto(ball) {
    $("#lotteryname", window.parent.document).html("极速赛车");
    $.post("class/auto_jssc.php", {ball: ball}, function (data) {
        //console.log(data);
        //查找当前框架的父级框架并修改父级框架的元素
        $("#qihao", window.parent.document).html(data.numbers + "期开奖");
        //获取下一期的开奖期号
        var openqihao = $("#open_qihao").html();
        if (auto_new == false || openqihao - data.numbers == 1) {
            //循环拼接开奖结果html代码，并推送到父级框架指定位置显示
            var numinfo = '';
            for (var b = 0; b < data.hms.length; b++) {
                numinfo += '<div class="virtual">' + data.hms[b] + '</div>';
            }
            $("#result", window.parent.document).html(numinfo);
            //循环拼接开奖球号html代码，并推送到父级框架指定位置显示
            var qiuhao = '';
            for (var i = 0; i < data.hm.length; i++) {
                qiuhao += '<b class="diamond b' + data.hm[i] + '"></b>';
            }

            var result_balls_HTML = '<li class="clear" id="qiuhao">' + qiuhao + '</li>';
            result_balls_HTML += '<li class="clear" id="result">' + numinfo + '</li>';
            $("#result_balls", window.parent.document).html(result_balls_HTML);
        }
    }, "json");
}

//关闭赔率及金额
function close_td() {
    $(".bottom-brass input[type='text']").val("");
    $(".min-tab-td1").html("-");
    $(".max-tab-td1").html("-");
    $(".remove_gan").html("");
    $(".min-tab-td2").html("");
    $(".max-tab-td2").html("");
}

//读取第几球
function did(type) {
    var r = '';
    switch (type) {
        case 1 :
            r = '冠军';
            break;
        case 2 :
            r = '亚军';
            break;
        case 3 :
            r = '第三名';
            break;
        case 4 :
            r = '第四名';
            break;
        case 5 :
            r = '第五名';
            break;
        case 6 :
            r = '第六名';
            break;
        case 7 :
            r = '第七名';
            break;
        case 8 :
            r = '第八名';
            break;
        case 9 :
            r = '第九名';
            break;
        case 10 :
            r = '第十名';
            break;
        case 11 :
            r = '冠亚军和';
            break;
    }
    return r;
}

//读取玩法
function wan(type) {
    var r = '';
    switch (type) {
        case 1 :
            r = '一号车';
            break;
        case 2 :
            r = '二号车';
            break;
        case 3 :
            r = '三号车';
            break;
        case 4 :
            r = '四号车';
            break;
        case 5 :
            r = '五号车';
            break;
        case 6 :
            r = '六号车';
            break;
        case 7 :
            r = '七号车';
            break;
        case 8 :
            r = '八号车';
            break;
        case 9 :
            r = '九号车';
            break;
        case 10 :
            r = '十号车';
            break;
        case 11 :
            r = '大';
            break;
        case 12 :
            r = '小';
            break;
        case 13 :
            r = '单';
            break;
        case 14 :
            r = '双';
            break;
        case 15 :
            r = '龙';
            break;
        case 16 :
            r = '虎';
            break;
    }
    return r;
}

//读取玩法
function wan9(type) {
    var r = '';
    switch (type) {
        case 1 :
            r = '3';
            break;
        case 2 :
            r = '4';
            break;
        case 3 :
            r = '5';
            break;
        case 4 :
            r = '6';
            break;
        case 5 :
            r = '7';
            break;
        case 6 :
            r = '8';
            break;
        case 7 :
            r = '9';
            break;
        case 8 :
            r = '10';
            break;
        case 9 :
            r = '11';
            break;
        case 10 :
            r = '12';
            break;
        case 11 :
            r = '13';
            break;
        case 12 :
            r = '14';
            break;
        case 13 :
            r = '15';
            break;
        case 14 :
            r = '16';
            break;
        case 15 :
            r = '17';
            break;
        case 16 :
            r = '18';
            break;
        case 17 :
            r = '19';
            break;
        case 18 :
            r = '大';
            break;
        case 19 :
            r = '小';
            break;
        case 20 :
            r = '单';
            break;
        case 21 :
            r = '双';
            break;
    }
    return r;
}


//开始加载
loadinfo(); //加载数据项
$(function () {
    $('.alert-mask').click(function () {
        var min = min_money;
        cou = m = 0, txt = '', c = true;
        var res = {};
        var json = [];
        var one_money;
        for (var i = 1; i < 12; i++) {
            if (i == 11) {
                for (var s = 1; s < 22; s++) {
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {
                        one_money = parseInt($("#ball_" + i + "_" + s).val());
                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_" + i + "_h" + s).html();
                        var q = did(i);
                        var w = wan9(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            } else {
                for (var s = 1; s < 17; s++) {
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {

                        one_money = parseInt($("#ball_" + i + "_" + s).val());
                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_1_h" + s).html();
                        var q = did(i);
                        var w = wan(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            }
        }

        if (cou <= 0 || is_fenpan) {
            $("#consult", window.parent.document).html('当前已封盘或输入的金额不正确，请重新选择！');
            $(".mask-two", window.parent.document).show();
        } else {
            $("#confirm_result", window.parent.document).append('<tr><th>号码</th><th>赔率</th><th>金额</th><th>确认</th></tr>');
            //循环遍历展示数据
            for (var i = 0; i < cou; i++) {
                $("#confirm_result", window.parent.document).append('<tr class="lines"><th>' + '<label>' + json[i]['qiu'] + '</label>&nbsp;<span>' + json[i]['wan'] + '</span></th><th><dt>' + json[i]['odds'] + '</dt></th><th><input type="text" id=' + json[i]['ball_id'] + ' name=' + json[i]['ball_id'] + ' value="' + json[i]['one_money'] + '"   maxlength="5" readonly></th><th><input type="checkbox" class="my_check" checked></th></tr>');
            }
            $("#confirm_result", window.parent.document).append('<tr><th colspan="2" >数组:' + '<span id="cou">' + cou + '</span></th><th colspan="2">总金额：' + '<span id="total_money">' + m + '</span></th></tr>');
            $(".mask", window.parent.document).show();

            //修改输入框时总金额随之改变
            $("input[type=text]", window.parent.document).change(function () {
                var value1 = 0;
                $("input[type=text]", window.parent.document).each(function () {

                    value1 += $(this).val() * 1;//框的值
                })
                $(this).parent().parent().siblings().find('#total_money').text(value1);//需要被修改的值
            })


            //点击删除复选框
            $("input[type=checkbox]", window.parent.document).click(function () {

                var value1 = $(this).parent().parent().siblings().find('#total_money').text() * 1;//总金额
                var value2 = $(this).parent().siblings().find('input[type=text]').val();
                var value3 = value1 - value2;
                $(this).parent().parent().siblings().find('#total_money').text(value3);//需要被修改的值
                value2 = $(this).parent().parent().siblings().find('#cou').text() * 1 - 1;
                $(this).parent().parent().siblings().find('#cou').text(value2);//数组值
                //移除选中的内容
                $(this).parent().parent().remove();
                if (value2 == '0') {
                    $('.close', window.parent.document).trigger("click");
                }


            });
            //将当前页需要传值的页面赋值给父框架
            $("#orders", window.parent.document).attr("action", "class/order_jssc.php");
        }
        $('.reset').trigger("click");
    });

    //进入页面获取默认数据
    get_jsscresult(1);

    //获取后台对应的数据
    function get_jsscresult(re) {

        $.get("class/get_jsscresult.php?ball=" + re, function (data) {
            var html = "";
            //console.log(data);
            for (var i = 0; i < 10; i++) {
                html += "<td>" + data[i] + "</td>";
            }
            $("#result").html(html);
        }, "json");
    }

    //点击按钮样式及数据改变
    $('#cg tr td').click(function () {

        if (!$(this).is(".bjs")) {

            $(this).addClass("bjs");
            $(this).siblings().removeClass("bjs");

            var index = $('#cg tr td').index(this);

            get_jsscresult(index + 1);
        }

    });


    //给冠亚军/龙虎添加字体颜色
    function add_color(i) {

        if (i == "大" || i == "双" || i == "龙") {

            return '<span style="color:red">' + i + '</span>';
        } else {

            return '<span style="color:black">' + i + '</span>';
        }
    }

    //获取后台走势对应数据(号码)
    function get_jssctrend(val) {

        $.get("./class/get_jssctrend.php?val=" + val, function (data) {

            //console.log(val);
            if (val == 1) {//获取后台走势对应数据(号码)

                for (var j = 0; j < data.length; j++) {

                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j].qishu + '</th><th>' + data[j].datetime +
                        '</th><td><b class="diamond b' + data[j].ball_1 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_2 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_3 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_4 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_5 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_6 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_7 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_8 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_9 + '"></b></td><td>' +
                        '</td><td><b class="diamond b' + data[j].ball_10 + '"></b></td><td>' +
                        '</tr>');
                }

            } else if (val == 2 || val == 3) {//获取后台走势对应数据(单双/大小)

                for (var j = 0; j < data.length; j++) {

                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j].qishu + '</th><th>' + data[j].datetime +
                        '</th><td><b class="dxds b' + data[j].ball_1 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_2 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_3 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_4 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_5 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_6 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_7 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_8 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_9 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_10 + '"></b></td><td>' +
                        '</tr>');
                }
            } else if (val == 4) {//获取后台走势对应数据(冠亚军/龙虎)

                for (var j = 0; j < data.length; j++) {

                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j][0] + '</th><th>' + data[j][1] +
                        '</th><td>' + add_color(data[j][2]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][3]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][4]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][5]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][6]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][7]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][8]) + '</td><td>' +
                        '</td><td>' + add_color(data[j][9]) + '</td><td>' +
                        '</tr>');
                }

            }

        }, "json");
    }

    //结果走势弹出框
    $('.jgzs').click(function () {
        //将对应的标题数据赋值给父框架
        $(".bt", window.parent.document).find('span').eq(0).text('极速赛车 - 结果走势');
        $(".item", window.parent.document).find('a').eq(0).text('号码');
        $(".item", window.parent.document).find('a').eq(1).text('大小');
        $(".item", window.parent.document).find('a').eq(2).text('单双');
        $(".item", window.parent.document).find('a').eq(3).text('冠亚军/龙虎');
        //获取初始默认数据
        get_jssctrend(1);
        //点击按钮样式及数据改变
        $(".item", window.parent.document).find('a').click(function () {
            //获取点击数据对应编号
            var index = $(".item", window.parent.document).find('a').index(this);

            $("#trend", window.parent.document).find('tr').remove();

            get_jssctrend(index + 1);

        });
        $(".results-the-trend", window.parent.document).show();
    });

    /*
        $('.dise').hover(function () {
            $('.dise').addClass('hover');
        }, function () {
            $('.dise').removeClass('hover');
        })
        $('.dise2').hover(function () {
            $('.dise2').addClass('hover');
        }, function () {
            $('.dise2').removeClass('hover');
        })
        $('.dise3').hover(function () {
            $('.dise3').addClass('hover');
        }, function () {
            $('.dise3').removeClass('hover');
        })
        $('.dise4').hover(function () {
            $('.dise4').addClass('hover');
        }, function () {
            $('.dise4').removeClass('hover');
        })*/
});